<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2014 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: Dean <zxxjjforever@163.com>
// +----------------------------------------------------------------------

namespace plugins\dingding\controller;

use cmf\controller\PluginAdminBaseController;
use plugins\dingding\lib\Dingding;
use plugins\dingding\model\PluginDingdingSenseModel;
use think\Db;

class AdminSenseController extends PluginAdminBaseController
{
    protected function initialize()
    {
        parent::initialize();
        $adminId = cmf_get_current_admin_id(); //获取后台管理员id，可判断是否登录
        if (!empty($adminId)) {
            $this->assign('admin_id', $adminId);
        }
    }

    public function index()
    {

            $corp = $this->request->param("corp");
            $app = $this->request->param('app');
            $apps = Db::name('plugin_dingding_sense')->where(['corp'=>$corp,'app'=>$app])->select();

        $this->assign('apps', $apps);

        return $this->fetch();
    }

    public function Add()
    {
        return $this->fetch();
    }

    public function AddPost()
    {
        $corp = $this->request->param("corp");
        $app = $this->request->param('app');
        if ($this->request->isPost()) {
            $data   = $this->request->param();
            //$result = $this->validate($data, 'app');
            $result = true;
            if ($result !== true) {
                // 验证失败 输出错误信息
                $this->error($result);
            } else {
                $app_model = new PluginDingdingSenseModel();
                $result = $app_model->allowField(true)->save($data);
                if ($result) {
                    $this->success("添加场景成功", cmf_plugin_url("dingding://AdminSense/index"));
                } else {
                    $this->error("添加场景失败");
                }
            }
        }
    }
    public function Edit()
    {
        $id = $this->request->param("id", 0, 'intval');

        $data = Db::name('plugin_dingding_sense')->where("id", $id)->find();
        if (!$data) {
            $this->error("该场景不存在！");
        }
        $this->assign("data", $data);
        return $this->fetch();
    }

    public function EditPost()
    {
        $id = $this->request->param("id", 0, 'intval');
        $corp = $this->request->param("corp");
        $app = $this->request->param('app');
        //$sense = Db::name('plugin_dingding_sense')->where('id',$id)->find();
        if ($this->request->isPost()) {
            $data   = $this->request->param();
            //$result = $this->validate($data, 'app');
            $result = true;
            if ($result !== true) {
                // 验证失败 输出错误信息
                $this->error($result);

            } else {
                $app_model = new PluginDingdingSenseModel();
                if ($app_model->allowField(true)->isUpdate(true)->save($data) !== false) {
                    $this->success("保存成功！", cmf_plugin_url('dingding://AdminSense/index',['corp'=>$corp,'app'=>$app]));
                } else {
                    $this->error("保存失败！");
                }
            }
        }
    }
    public function Delete()
    {
        $id = $this->request->param("id", 0, 'intval');

            $status = Db::name('plugin_dingding_sense')->delete($id);
            if (!empty($status)) {
                $this->success("删除成功！", cmf_plugin_url('dingding://AdminSense/index'));
            } else {
                $this->error("删除失败！");
            }

    }

    public function upStatus()
    {
        $intId     = $this->request->param("id");
        $intStatus = $this->request->param("status");
        $intStatus = $intStatus ? 1 : 0;
        if (empty($intId)) {
            $this->error(lang("NO_ID"));
        }

        $portalTagModel = new PluginDingdingSenseModel();
        $portalTagModel->isUpdate(true)->save(["status" => $intStatus], ["id" => $intId]);

        $this->success(lang("SAVE_SUCCESS"));

    }

    public function fieldsetting()
    {

        $id = $this->request->param("id", 0, 'intval');
        $sense = Db::name('plugin_dingding_sense')->where("id", $id)->find();
        if (!$sense) {
            $this->error("该场景不存在！");
        }

        $sense_fields = $this->get_process_template($sense['corp'],$sense['app'],$sense['procode']);

        $sense['listfield'] = empty($sense['listfield'])? $sense['listfield'] : json_decode($sense['listfield'],true);
        $sense['excelfield'] = empty($sense['excelfield'])? $sense['excelfield'] : json_decode($sense['excelfield'],true);
        $sense['downfield'] = empty($sense['downfield'])? $sense['downfield'] : json_decode($sense['downfield'],true);
        $this->assign('fields',$sense_fields);
        $this->assign('sense',$sense);


        return $this->fetch();
    }

    public function fieldsettingPost(){
        $id = $this->request->param("id", 0, 'intval');
        $corp = $this->request->param("corp");
        $app = $this->request->param('app');
        if ($this->request->isPost()) {
            $data   = $this->request->param();
            //$result = $this->validate($data, 'app');
            $result = true;
            if ($result !== true) {
                // 验证失败 输出错误信息
                $this->error($result);

            } else {
                $app_model = new PluginDingdingSenseModel();
                $data['listfield'] = empty($data['listfield'])? '':json_encode($data['listfield']);
                $data['excelfield'] = empty($data['excelfield'])? '':json_encode($data['excelfield']);
                $data['downfield'] = empty($data['downfield'])? '':json_encode($data['downfield']);
                if ($app_model->allowField(true)->isUpdate(true)->save($data) !== false) {
                    $this->success("保存成功！", cmf_plugin_url('dingding://AdminSense/index',['corp'=>$corp,'app'=>$app]));
                } else {
                    $this->error("保存失败！");
                }
            }
        }
    }

    public function downsetting(){

    }

    protected function get_process_template($corp,$app,$procode){
//        $sense = Db::name('plugin_dingding_sense')->where('sense','ruoguang')->find();
        $ding = new Dingding($corp,$app);
        $access_token = $ding->getAccessToken();
        require_once("../vendor/dingding/TopSdk.php");
        $c = new \DingTalkClient(\DingTalkConstant::$CALL_TYPE_OAPI, \DingTalkConstant::$METHOD_POST , \DingTalkConstant::$FORMAT_JSON);

        $req = new \OapiProcessFormGetRequest();
        $req->setProcessCode($procode);
        $resp=$c->execute($req, $access_token,"https://oapi.dingtalk.com/topapi/process/form/get");
        foreach ($resp->result->form_component_vos as $arr){
            $result[] = $arr->props->label;
        }
        return $result;
    }
}
